/**
 * 
 */
package info.niwota.webapp.acl;

import info.niwota.commons.Debug;
import info.niwota.commons.acl.AccessPermission;
import info.niwota.commons.web.LabelColumns;
import info.niwota.commons.web.MatrixCursor;

import java.util.Arrays;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
abstract public class LabelContentProvider extends ContentProvider {

	private class MyMatrixCursor extends MatrixCursor {
		public MyMatrixCursor(String[] columnNames) throws Exception {
			super(columnNames);

			load();
		}

		protected void load() throws Exception {
			rowCount = 0;
			//
			int size = 0;
			data = new Object[size * columnCount];
			//
			AccessPermission[] perms = loadPerms();
			if (perms == null || perms.length == 0) {
				return;
			}
			for (int i = 0; i < perms.length; i++) {
				AccessPermission p = perms[i];
				int id = (p.id == -1 ? i  : p.id);
				if (Debug.DEBUG) {
					Log.d(TAG, "id: " + id + " file: " + p);
				}
				String icon = ( p.icon == null ? null : p.icon.getPath());
				Object[] row = new Object[] { id, p.label, icon, p.desc, p.perm,
						p.emails };
				//
				addRow(row);
			}
		}

		@Override
		public boolean requery() {
			if (Debug.DEBUG) {
				Log.d(TAG, "requerying...");
			}
			try {
				load();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			return super.requery();
		}
	}

	private static final String TAG = "LabelContentProvider";

	@Override
	public int bulkInsert(Uri uri, ContentValues[] values) {
		return 0;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String getType(Uri uri) {
		return "vnd.android.cursor.dir/x-label";
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		throw new UnsupportedOperationException();
	}

	/**
	 * @return access permission for all labels
	 */
	protected AccessPermission[] loadPerms() {
		return null;
	}

	@Override
	public boolean onCreate() {
		return true;
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		if (Debug.DEBUG) {
			Log.d(TAG, "query: "
					+ uri
					+ " projection: "
					+ (projection == null ? "" : Arrays.asList(projection))
					+ " selection: "
					+ selection
					+ " args: "
					+ (selectionArgs == null ? "" : Arrays
							.asList(selectionArgs)) + " sortOrder: "
					+ sortOrder);
		}

		try {
			MatrixCursor c = new MyMatrixCursor(LabelColumns.ColumnNames);
			c.setNotificationUri(getContext().getContentResolver(), uri);
			return c;
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Override this method to persist the values
	 * 
	 * @param label
	 * @param perm
	 * @param emails
	 */
	protected void store(String label, String perm, String emails) {
		//
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		if (Debug.DEBUG) {
			Log.d(TAG, "update: "
					+ uri
					+ " values: "
					+ values
					+ " selection: "
					+ selection
					+ " args: "
					+ (selectionArgs == null ? "" : Arrays
							.asList(selectionArgs)));
		}

		String label = values.getAsString(LabelColumns.LABEL);
		String perm = values.getAsString(LabelColumns.PERM);
		String emails = values.getAsString(LabelColumns.EMAILS);
		
		store(label, perm, emails);
		
		return 1;
	}

}
